(function () {
  var stateMap, elemMap, setElemMap, bindElemEvent, fetchApps, initPage,
    handleClickReplyList, renderAppList, handleClickServer;

  //初始化页面元素的一些状态
  initPage = function () {
    if (localStorage.jqyPhone) {
      elemMap.myName.innerHTML = util.hidePhone(localStorage.jqyPhone);
      elemMap.myName.setAttribute('href', 'javascript:;');
    } else {
      elemMap.myName.addEventListener('click', function (e) {
        e.preventDefault();
        /*************S 记录点击登录注册************/
        var temp = JSON.parse(localStorage.TJBasic);
        temp.CurrentPage = '我';
        temp.time = (new Date()).valueOf();
        temp.behavior = '点击登录/注册';
        temp.sfLogin = localStorage.jqyPhone ? '是' : '否';
        temp.loginType = '密码登录';
        var arr = JSON.parse(localStorage.TJArray);
        arr.push(temp);
        localStorage.TJArray = JSON.stringify(arr);
        /*************E 记录点击登录注册************/
        location.href = this.getAttribute('href');
      });
    }
  };

  //获取应用推荐
  fetchApps = function () {
    $.ajax({
      url: util.hostUrl + '/v1/application/Application.html',
      type: 'post',
      data: {
        header: JSON.stringify({
          reqKey: util.reqKey
        }),
        param: JSON.stringify({
          app: util.app
        })
      },
      success: function (res) {
        if (res.result.code == 1) {
          renderAppList(res.response.dataList);
        } else {
          util.toast('Oops,发生错误！');
        }
      },
      error: function () {
        util.toast('Oops,网络有问题！');
      }
    });
  };

  //渲染应用推荐
  renderAppList = function (arr) {
    var temp = arr.map(function (val) {
      return String()
        + '<div class="swiper-slide" style="background-image: url('+ val.logo +')">'
          +'<a href="'+ val.url +'"><p>'+ val.name +'</p></a>'
        + '</div>';
    });
    elemMap.appWrap.innerHTML = temp.join('');

    new Swiper(elemMap.appContainer, {
      slidesPerView: 4
    });
  };

  /************B 事件处理函数************/
  handleClickReplyList = function (e) {
    e.preventDefault();
    if (!localStorage.jqyPhone) {
      location.href = './signIn.html?to=reply';
    } else {
      location.href = e.currentTarget.getAttribute('href');
    }
  };

  handleClickServer = function (e) {
    e.preventDefault();
    if (!localStorage.jqyPhone) {
      location.href = './signIn.html?to=me';
    } else {
      location.href = e.currentTarget.getAttribute('href');
    }
  };
  /************E 事件处理函数************/

  bindElemEvent = function () {
    elemMap.reply.addEventListener('click', handleClickReplyList);
    elemMap.server.addEventListener('click', handleClickServer);
  };

  setElemMap = function () {
    elemMap = {
      myName    : document.querySelector('.my-name a'),
      reply     : document.querySelector('.reply a'),
      appContainer: document.querySelector('.app-container'),
      appWrap   : document.querySelector('.swiper-wrapper'),
      server    : document.querySelector('.server a')
    }
  };

  setElemMap();
  bindElemEvent();
  initPage();
  fetchApps();
  util.setPageHeight();
}());
